package handler

import (
	"github.com/gin-gonic/gin"
	"github.com/lab-online/internal/user/constant"
	"github.com/lab-online/internal/user/entity"
	"github.com/lab-online/pkg/auth"
	"github.com/lab-online/pkg/resp"
)

type GetProfileRespData struct {
	Username string `json:"username" binding:"required" example:"20240126"` // 学号或工号
	Name     string `json:"name" binding:"required" example:"杏鸣"`           // 姓名
	Role     uint8  `json:"role" binding:"required" example:"1"`            // 角色
}
type GetProfileRespBody struct {
	resp.BaseRespBody
	Data GetProfileRespData `json:"data" binding:"required"`
}

// GetProfile
//
//	@Summary				用户信息
//	@Description.markdown	GetProfile
//	@Tags					user
//	@Accept					json
//	@Produce				json
//	@Param					Authorization	header		string	true	"Bearer Token"
//	@Success				200				{object}	GetProfileRespBody
//	@Failure				400				{object}	resp.BaseRespBody
//	@Failure				401				{object}	resp.BaseRespBody
//	@Security				bearer
//	@x-apifox-status		"testing"
//	@Router					/v1/users/profile [get]
func (h *Handler) GetProfile(c *gin.Context) {
	authEntity := c.MustGet(auth.KeyAuth).(auth.AuthEntity)

	userEntity := entity.NewUserEntity(entity.WithUsername(authEntity.GetUsername()))

	if user, err := h.domain.GetUser(userEntity); err != nil {
		resp.ErrorHandler(c, err)
	} else {
		resp.Success(c, constant.CodeGetProfileSuccess, &GetProfileRespData{
			Username: user.GetUsername(),
			Name:     user.GetName(),
			Role:     user.GetRole(),
		})
	}
}
